﻿/**
 * ...
 * @author alijaya
 */

package heox;
import heox.events.DimensionEvent;

class Rectangle extends Point
{

	public var initWidth(default, null):Float;
	public var initHeight(default, null):Float;
	
	//////////////////////////
	// Dimension Properties //
	//////////////////////////
	
	private var fixedWidth:Null<Float>;
	private var fixedHeight:Null<Float>;
	
	
	private var _width : Float;
	public var width(get_width, set_width) : Float;
	
	private function get_width() : Float
	{
		return _width;
	}
	
	private function set_width(value:Float) : Float
	{
		_width = (fixedWidth == null)?value:fixedWidth;
		if (_width < 0) _width = 0;
		update_width();
		dispatchEvent(new DimensionEvent(DimensionEvent.WIDTH, width, height));
		dispatchEvent(new DimensionEvent(DimensionEvent.WIDTH_OR_HEIGHT, width, height));
		return value;
	}
	
	private var _height : Float;
	public var height(get_height, set_height) : Float;
	
	private function get_height() : Float
	{
		return _height;
	}
	
	private function set_height(value:Float) : Float
	{
		_height = (fixedHeight == null)?value:fixedHeight;
		if (_height < 0) _height = 0;
		update_height();
		dispatchEvent(new DimensionEvent(DimensionEvent.HEIGHT, width, height));
		dispatchEvent(new DimensionEvent(DimensionEvent.WIDTH_OR_HEIGHT, width, height));
		return value;
	}
	
	public var top(get_top, set_top) : Float;
	
	private function get_top() : Float
	{
		return y;
	}
	
	private function set_top(value:Float) : Float
	{
		var n:Float = bottom;
		y = value;
		height = n - value;
		return value;
	}
	
	public var bottom(get_bottom, set_bottom) : Float;
	
	private function get_bottom() : Float
	{
		return y + height;
	}
	
	private function set_bottom(value:Float) : Float
	{
		height = value - y;
		return value;
	}
	
	public var left(get_left, set_left) : Float;
	
	private function get_left() : Float
	{
		return x;
	}
	
	private function set_left(value:Float) : Float
	{
		var n:Float = right;
		x = value;
		width = n - value;
		return value;
	}
	
	public var right(get_right, set_right) : Float;
	
	private function get_right() : Float
	{
		return x + width;
	}
	
	private function set_right(value:Float) : Float
	{
		width = value - x;
		return value;
	}
	
	public function new(?x:Float = 0, ?y:Float = 0, ?width:Float = 0, ?height:Float = 0) 
	{
		super(x, y);
		this.initWidth = 0;
		this.initHeight = 0;
		this.fixedWidth = null;
		this.fixedHeight = null;
		this._width = width;
		this._height = height;
	}
	
	public override function clone() : Point
	{
		return new Rectangle(this.x, this.y, this.width, this.height);
	}
	
	public override function toInit()
	{
		super.toInit();
		setDim(initWidth, initHeight);
	}
	
	public function setDim(width:Float, height:Float)
	{
		this.width = width;
		this.height = height;
		dispatchEvent(new DimensionEvent(DimensionEvent.WIDTH_AND_HEIGHT, width, height));
	}
	
	public function setPosDim(x:Float, y:Float, width:Float, height:Float)
	{
		setPos(x, y);
		setDim(width, height);
	}
	
	
	
	public static function intersects(r1:Rectangle, r2:Rectangle) : Bool
	{
		return (r1.top < r2.bottom && r1.bottom > r2.top && r1.left < r2.right && r1.right > r2.left);
	}
	
	
	
	
	public function isEmpty() : Bool
	{
		return (width <= 0 || height <= 0);
	}
	
	
	#if flash9
	public static function flashRectangle(r:Rectangle) : flash.geom.Rectangle
	{
		return new flash.geom.Rectangle(r.x, r.y, r.width, r.height);
	}
	
	public static function haxeRectangle(r:flash.geom.Rectangle) : Rectangle
	{
		return new Rectangle(r.x, r.y, r.width, r.height);
	}
	#end
	
	/////////////////////
	// Update Function //
	/////////////////////
	
	private function update_width()
	{
		
	}
	
	private function update_height()
	{
		
	}
	
	public override function toString() : String
	{
		var s:String = "{ x:" + x + ", y:" + y + ", width:" + width + ", height:" + height + " }";
		return s;
	}
	
}